In [1]:
%matplotlib inline
%pylab inline


Populating the interactive namespace from numpy and matplotlib

Load training data


In [3]:
import menpo.io as mio
from menpo.landmark import labeller, streetscene_car_view_0
from menpofast.utils import convert_from_menpo

group = 'streetscene_car_view_0'

training_images = []
for i in mio.import_images('/data/PhD/DataBases/cars/cmu_car_data1/view0/', 
                           verbose=True, max_images=200):
    
    # convert the image from menpo Image to menpofast Image (channels at front)
    i = convert_from_menpo(i)
    
    labeller(i, 'PTS', eval(group))
    i.crop_to_landmarks_proportion_inplace(1, group=group)
    i = i.rescale_landmarks_to_diagonal_range(200, group=group)
    
    if i.n_channels == 3:
        i = i.as_greyscale(mode='average')
    training_images.append(i)


- Loading 200 assets: [====================] 100%

In [4]:
from menpo.visualize import visualize_images

visualize_images(training_images)


Define features


In [5]:
from functools import partial
from menpofast.feature import dsift

aam_dsift = partial(dsift, fast=True, window_size=5, geometry=(1, 1, 8))
aam_dsift2 = partial(dsift, fast=True, window_size=5, geometry=(2, 2, 8))

In [6]:
%timeit aam_dsift(i)
%timeit aam_dsift2(i)


10 loops, best of 3: 55.2 ms per loop
10 loops, best of 3: 167 ms per loop

In [7]:
img = training_images[0]
rescaled_img = img.rescale_landmarks_to_diagonal_range(50, group=group)
masked_img = rescaled_img.as_masked()
masked_img.build_mask_around_landmarks((15, 15), group=group)
masked_img.view_widget()



In [8]:
dsift_img = aam_dsift2(masked_img)
dsift_img.view_widget()


Active Appearance Models

Parts

Build


In [ ]:
from menpofast.feature import no_op, aam_dsift
from alabortcvpr2015.aam import PartsAAMBuilder

builder = PartsAAMBuilder(parts_shape=(15, 15), features=aam_dsift, diagonal=50, 
                          scale_shapes=False, scales=(1, .5))
aam = builder.build(training_images[100:], group=group, verbose=True)

Test


In [ ]:
sampling_mask = np.require(np.zeros((15, 15)), dtype=np.bool)
sampling_mask[1::2, 1::2] = True
#sampling_mask[:] = True

imshow(sampling_mask)

In [ ]:
from alabortcvpr2015.aam import PartsAAMFitter, AIC, PIC
                  
fitter = PartsAAMFitter(aam, algorithm_cls=AIC, n_shape=[3, 12], 
                        n_appearance=[25, 50], sampling_mask=sampling_mask)

In [ ]:
np.random.seed(seed=1)

fitter_results = []

for j, i in enumerate(training_images[100:]):
    
    gt_s = i.landmarks[group].lms
    s = fitter.perturb_shape(gt_s, noise_std=0.08, rotation=True)
    
    fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=20, prior=True)
    fr.downscale = 0.5
    
    fitter_results.append(fr)
    
    print 'Image: ', j
    print fr

Visualize Results


In [ ]:
from menpofit.visualize import visualize_fitting_results
    
visualize_fitting_results(fitter_results)

Timing

Total


In [ ]:
%timeit fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Per line


In [ ]:
import line_profiler
import IPython

ip = IPython.get_ipython()
ip.define_magic('lprun', line_profiler.magic_lprun)

In [ ]:
from alabortcvpr2015.aam import AIC

%lprun -f AIC.run fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Constrained Local Models

MCF Experts

Build


In [9]:
from menpofast.feature import no_op, aam_dsift, aam_daisy
from alabortcvpr2015.clm import CLMBuilder
from alabortcvpr2015.clm.classifier import MCF

offsets = np.meshgrid(range(-0, 1, 1), range(-0, 1, 1))
offsets = np.asarray([offsets[0].flatten(), offsets[1].flatten()]).T 

builder = CLMBuilder(parts_shape=(15, 15), features=aam_dsift, diagonal=50, 
                     classifier=MCF, offsets=offsets, normalize_parts=False, 
                     covariance=2, scale_shapes=False, scales=(1, .5))

clm = builder.build(training_images[:100], group=group, verbose=True, l=0.1)


- Building models
  - Level 0: Done
  - Level 1: Done

Test


In [13]:
from alabortcvpr2015.clm import CLMFitter, RLMS                  

fitter = CLMFitter(clm, n_shape=[3, 6], factor=10)

In [14]:
np.random.seed(seed=1)

fitter_results = []

for j, i in enumerate(training_images[100:200]):
    
    gt_s = i.landmarks[group].lms
    s = fitter.perturb_shape(gt_s, noise_std=0.08, rotation=True)
    
    fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=20, prior=False)
    
    fitter_results.append(fr)
    fr.downscale = 0.5
    
    print 'Image: ', j
    print fr


Image:  0
Initial error: 0.1198
Final error: 0.6445
Image:  1
Initial error: 0.1723
Final error: 0.8087
Image:  2
Initial error: 0.2240
Final error: 0.6960
Image:  3
Initial error: 0.1525
Final error: 0.5382
Image:  4
Initial error: 0.0564
Final error: 0.5909
Image:  5
Initial error: 0.0694
Final error: 0.5141
Image:  6
Initial error: 0.0620
Final error: 0.6359
Image:  7
Initial error: 0.1183
Final error: 0.5118
Image:  8
Initial error: 0.1343
Final error: 0.4878
Image:  9
Initial error: 0.0635
Final error: 0.5837
Image:  10
Initial error: 0.1466
Final error: 0.7105
Image:  11
Initial error: 0.1887
Final error: 0.6285
Image:  12
Initial error: 0.0479
Final error: 0.5550
Image:  13
Initial error: 0.1579
Final error: 0.5120
Image:  14
Initial error: 0.1492
Final error: 0.8104
Image:  15
Initial error: 0.1355
Final error: 0.5103
Image:  16
Initial error: 0.1871
Final error: 0.7619
Image:  17
Initial error: 0.1455
Final error: 0.8690
Image:  18
Initial error: 0.1994
Final error: 0.6139
Image:  19
Initial error: 0.0797
Final error: 0.5359
Image:  20
Initial error: 0.0438
Final error: 0.6905
Image:  21
Initial error: 0.1101
Final error: 0.5914
Image:  22
Initial error: 0.1253
Final error: 0.7148
Image:  23
Initial error: 0.0846
Final error: 0.4724
Image:  24
Initial error: 0.1192
Final error: 0.5453
Image:  25
Initial error: 0.0396
Final error: 0.5828
Image:  26
Initial error: 0.2012
Final error: 0.4548
Image:  27
Initial error: 0.1520
Final error: 0.4137
Image:  28
Initial error: 0.1121
Final error: 0.5980
Image:  29
Initial error: 0.0668
Final error: 0.6481
Image:  30
Initial error: 0.1790
Final error: 0.7162
Image:  31
Initial error: 0.1645
Final error: 0.4296
Image:  32
Initial error: 0.2018
Final error: 0.4312
Image:  33
Initial error: 0.1715
Final error: 0.4891
Image:  34
Initial error: 0.1094
Final error: 0.6524
Image:  35
Initial error: 0.0703
Final error: 0.5292
Image:  36
Initial error: 0.1424
Final error: 0.7669
Image:  37
Initial error: 0.2166
Final error: 0.4614
Image:  38
Initial error: 0.1214
Final error: 0.7023
Image:  39
Initial error: 0.1397
Final error: 0.5014
Image:  40
Initial error: 0.1126
Final error: 0.4766
Image:  41
Initial error: 0.1030
Final error: 0.6258
Image:  42
Initial error: 0.2670
Final error: 0.9496
Image:  43
Initial error: 0.2566
Final error: 0.8859
Image:  44
Initial error: 0.1548
Final error: 0.8155
Image:  45
Initial error: 0.1000
Final error: 0.4790
Image:  46
Initial error: 0.0600
Final error: 0.6309
Image:  47
Initial error: 0.0977
Final error: 0.5914
Image:  48
Initial error: 0.1735
Final error: 0.8025
Image:  49
Initial error: 0.1602
Final error: 0.7457
Image:  50
Initial error: 0.1522
Final error: 0.6445
Image:  51
Initial error: 0.3080
Final error: 0.2579
Image:  52
Initial error: 0.2572
Final error: 0.2890
Image:  53
Initial error: 0.1529
Final error: 0.7340
Image:  54
Initial error: 0.1478
Final error: 0.7175
Image:  55
Initial error: 0.2427
Final error: 0.9419
Image:  56
Initial error: 0.1990
Final error: 0.3898
Image:  57
Initial error: 0.0801
Final error: 0.4609
Image:  58
Initial error: 0.1109
Final error: 0.5999
Image:  59
Initial error: 0.2310
Final error: 0.4368
Image:  60
Initial error: 0.1873
Final error: 0.4611
Image:  61
Initial error: 0.1467
Final error: 0.4794
Image:  62
Initial error: 0.1626
Final error: 0.7721
Image:  63
Initial error: 0.3996
Final error: 0.2452
Image:  64
Initial error: 0.2063
Final error: 0.8995
Image:  65
Initial error: 0.0699
Final error: 0.6361
Image:  66
Initial error: 0.0475
Final error: 0.5828
Image:  67
Initial error: 0.1615
Final error: 0.7449
Image:  68
Initial error: 0.0470
Final error: 0.6286
Image:  69
Initial error: 0.1480
Final error: 0.5057
Image:  70
Initial error: 0.0870
Final error: 0.5148
Image:  71
Initial error: 0.0547
Final error: 0.6242
Image:  72
Initial error: 0.1502
Final error: 0.3548
Image:  73
Initial error: 0.0319
Final error: 0.6489
Image:  74
Initial error: 0.1012
Final error: 0.6111
Image:  75
Initial error: 0.1249
Final error: 0.7835
Image:  76
Initial error: 0.1036
Final error: 0.4281
Image:  77
Initial error: 0.1304
Final error: 0.6238
Image:  78
Initial error: 0.3551
Final error: 0.9275
Image:  79
Initial error: 0.0642
Final error: 0.6091
Image:  80
Initial error: 0.1034
Final error: 0.5473
Image:  81
Initial error: 0.1438
Final error: 0.7262
Image:  82
Initial error: 0.0815
Final error: 0.6385
Image:  83
Initial error: 0.1581
Final error: 0.5239
Image:  84
Initial error: 0.4988
Final error: 0.1237
Image:  85
Initial error: 0.0958
Final error: 0.3907
Image:  86
Initial error: 0.0742
Final error: 0.4757
Image:  87
Initial error: 0.1390
Final error: 0.7623
Image:  88
Initial error: 0.1118
Final error: 0.5072
Image:  89
Initial error: 0.0729
Final error: 0.5577
Image:  90
Initial error: 0.1981
Final error: 0.5151
Image:  91
Initial error: 0.3079
Final error: 0.4571
Image:  92
Initial error: 0.0707
Final error: 0.6396
Image:  93
Initial error: 0.2054
Final error: 0.7883
Image:  94
Initial error: 0.2297
Final error: 0.9267
Image:  95
Initial error: 0.1118
Final error: 0.6517
Image:  96
Initial error: 0.0662
Final error: 0.5493
Image:  97
Initial error: 0.1971
Final error: 0.4987
Image:  98
Initial error: 0.1350
Final error: 0.5155
Image:  99
Initial error: 0.1752
Final error: 0.4410

Visualize Results


In [15]:
from menpofit.visualize import visualize_fitting_results
    
visualize_fitting_results(fitter_results, colours='r', linewidths=2)


Timing

Total


In [ ]:
%timeit fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Per line


In [ ]:
import line_profiler
import IPython

ip = IPython.get_ipython()
ip.define_magic('lprun', line_profiler.magic_lprun)

In [ ]:
from alabortcvpr2015.clm import RLMS

%lprun -f RLMS.run fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Unified

Global

Build


In [ ]:
from menpofast.feature import no_op, aam_dsift, aam_daisy
from alabortcvpr2015.unified import GlobalUnifiedBuilder
from alabortcvpr2015.clm.classifier import MCF

offsets = np.meshgrid(range(-0, 1, 1), range(-0, 1, 1))
offsets = np.asarray([offsets[0].flatten(), offsets[1].flatten()]).T 

builder = GlobalUnifiedBuilder(parts_shape=(15, 15), features=aam_dsift, diagonal=100, 
                               classifier=MCF, offsets=offsets, normalize_parts=False, 
                               covariance=2, scale_shapes=False, scales=(1, .5))

unified = builder.build(training_images, group='ibug_face_66', verbose=True)

Test


In [ ]:
from alabortcvpr2015.unified import GlobalUnifiedFitter 
                  
fitter = GlobalUnifiedFitter(unified, n_shape=[3, 12], n_appearance=[25, 50], 
                             scale=1000, factor=10, sampling_step=8)

In [ ]:
np.random.seed(seed=1)

fitter_results = []

for j, i in enumerate(test_images):
    
    gt_s = i.landmarks['ibug_face_66'].lms
    s = fitter.perturb_shape(gt_s, noise_std=0.05)
    
    fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=20, prior=True)
    
    fitter_results.append(fr)
    fr.downscale = 0.5
    
    print 'Image: ', j
    print fr

Visualize Results


In [ ]:
from menpofit.visualize import visualize_fitting_results
    
visualize_fitting_results(fitter_results)

Timing

Total


In [ ]:
%timeit fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Per line


In [ ]:
import line_profiler
import IPython

ip = IPython.get_ipython()
ip.define_magic('lprun', line_profiler.magic_lprun)

In [ ]:
from alabortcvpr2015.unified import ProbAIC

%lprun -f ProbAIC.run fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Parts

Build


In [ ]:
from menpofast.feature import no_op, aam_dsift, aam_daisy
from alabortcvpr2015.unified import PartsUnifiedBuilder
from alabortcvpr2015.clm.classifier import MCF

offsets = np.meshgrid(range(-0, 1, 1), range(-0, 1, 1))
offsets = np.asarray([offsets[0].flatten(), offsets[1].flatten()]).T 

builder = PartsUnifiedBuilder(parts_shape=(15, 15), features=aam_dsift, diagonal=100, 
                              classifier=MCF, offsets=offsets, normalize_parts=False, 
                              covariance=2, scale_shapes=False, scales=(1, .5))

unified = builder.build(training_images, group='ibug_face_66', verbose=True)

Test


In [ ]:
sampling_mask = np.require(np.zeros((15, 15)), dtype=np.bool)
sampling_mask[1::4, 1::4] = True
#sampling_mask[:] = True

imshow(sampling_mask)

In [ ]:
from alabortcvpr2015.unified import PartsUnifiedFitter 
                  
fitter = PartsUnifiedFitter(unified, n_shape=[3, 12], n_appearance=[25, 50], 
                            scale=100000, factor=100, sampling_mask=sampling_mask)

In [ ]:
fitter._algorithms[0]._sigma2=10000
fitter._algorithms[1]._sigma2=10000
fitter._algorithms[0]._rho2=0
fitter._algorithms[1]._rho2=0

In [ ]:
np.random.seed(seed=1)

fitter_results = []

for j, i in enumerate(test_images):
    
    gt_s = i.landmarks['ibug_face_66'].lms
    s = fitter.perturb_shape(gt_s, noise_std=0.05)
    
    fr = fitter.fit(i, s, gt_shape=gt_s, max_iters=20, prior=False)
    
    fitter_results.append(fr)
    fr.downscale = 0.5
    
    print 'Image: ', j
    print fr

Visualize Results


In [ ]:
from menpofit.visualize import visualize_fitting_results
    
visualize_fitting_results(fitter_results)

Timing

Total


In [ ]:
%timeit fitter.fit(i, s, gt_shape=gt_s, max_iters=20)

Per line


In [ ]:
import line_profiler
import IPython

ip = IPython.get_ipython()
ip.define_magic('lprun', line_profiler.magic_lprun)

In [ ]:
from alabortcvpr2015.unified import ProbAIC

%lprun -f ProbAIC.run fitter.fit(i, s, gt_shape=gt_s, max_iters=20)